/*
 * Created by SharpDevelop.
 * user: Robertux
 * Date: 26/04/2008
 * Time: 18:42
 * 
 * 
 */

using System;
using DataLayer;
using MySql.Data.MySqlClient;
using System.Collections.Generic;

namespace Inventador
{
	/// <summary>
	/// Description of Area.
	/// </summary>
	public class Area
	{
		#region Campos
		
		private int _id;
		private string _nombre;
		
		#endregion
		
		#region Propiedades
		
		public int Id
		{
			get { return this._id; }
			set { this._id = value; }
		}
		
		public string Nombre
		{
			get { return this._nombre; }
			set { this._nombre = value; }
		}
		
		#endregion
		
		#region Metodos
		
		public Area(int pId, string pNombre)
		{
			this.Id = pId;
			this.Nombre = pNombre;
		}
		
		public bool GetFromDb(BaseDatos bdatos)
		{
			bdatos = new BaseDatos();
			bdatos.Conectar();
			MySqlDataReader lector = bdatos.EjecutarConsulta("SELECT * FROM area AS ar WHERE id=" + this.Id.ToString());
			if(lector.HasRows)
			{
				lector.Read();
				this.Id = int.Parse(lector["id"].ToString());
				this.Nombre = lector["nombre"].ToString();
				bdatos.Desconectar();
				return true;
			}
			bdatos.Desconectar();
			return false;
		}
		
		public static List<Area> GetList(BaseDatos bdatos, string condicion)
		{
			bdatos = new BaseDatos();
			List<Area> lst = new List<Area>();
			string consulta = "SELECT * FROM area AS ar";
			if(condicion != "")
				consulta += " WHERE " + condicion;
			bdatos.Conectar();
			MySqlDataReader lector = bdatos.EjecutarConsulta(consulta);
                	while (lector.Read())
                	{                    	                    	
                		lst.Add(new Area(int.Parse(lector["id"].ToString()), lector["nombre"].ToString()));
                	}
			bdatos.Desconectar();
            		return lst;
		}
		
		public void Insert(BaseDatos bdatos)
		{
			bdatos = new BaseDatos();
			bdatos.Conectar();
            		bdatos.EjecutarOperacion("INSERT INTO area VALUES(" + this.Id.ToString() + ", '" + this.Nombre + "' )");
			bdatos.Desconectar();
		}
		
		public void Update(BaseDatos bdatos)
		{
			bdatos = new BaseDatos();
			bdatos.Conectar();
			bdatos.EjecutarOperacion("UPDATE area SET nombre='" + this.Nombre
			+ "' WHERE id=" + this.Id.ToString());
			bdatos.Desconectar();
		}
		
		public void Delete(BaseDatos bdatos)
		{
			bdatos = new BaseDatos();
			bdatos.Conectar();
            		bdatos.EjecutarOperacion("DELETE FROM area WHERE id=" + this.Id.ToString());
			bdatos.Desconectar();
		}
		
		public static int GetMaxId(BaseDatos bdatos)
		{
			bdatos = new BaseDatos();
			bdatos.Conectar();
			int maxId = bdatos.EjecutarEscalar("SELECT MAX(id) FROM area");
			bdatos.Desconectar();
			return maxId;
		}
		
		#endregion
	}
}
